<template>
  <div class="container">
    <div class="header">
      <span @click="close(true)">返回</span>
      <div class="btn-action">
        <el-button
          size="medium"
          v-if="currentIndex !== 0"
          :disabled="currentIndex === 0"
          @click="currentIndex = currentIndex - 1"
        >上一步</el-button>

        <el-button
          size="medium"
          v-if="currentIndex !== stepList.length - 1 && courseInfo.id"
          :disabled="currentIndex === stepList.length - 1 || !courseInfo.id"
          @click="currentIndex = currentIndex + 1"
        >下一步</el-button>
      </div>
    </div>

    <div class="content">
      <div class="steps">
        <ul>
          <li
            v-for="(s, idx) in stepList" :key="s.t"
            :class="{ is__current: currentIndex === idx, is__complete: completeIndex >= idx }"
            :data-index="idx + 1"
            @click="currentIndex = idx"
          >
            <i class="el-icon-check" />
            <span>{{ s.t }}</span>
          </li>
        </ul>
      </div>
      <component :is="stepList[currentIndex].c" />
    </div>
  </div>
</template>

<script lang="ts">
import store from "./store";
import BasicComponent from "./components/basic.vue";
import CoursesComponent from "./components/courses.vue";
import SettingComponent from "./components/setting.vue";
import ReleaseComponent from "./components/release.vue";
import { computed } from "vue";
import axios from "axios";
export default {
  props: {
    close: Function,
    data: {
      type: Object,
      default: () => ({}),
    },
    currentIndex: {
      type: Number,
      default: 0,
    },
  },
  components: {
    BasicComponent,
    CoursesComponent,
    SettingComponent,
    ReleaseComponent,
  },
  setup(props) {
    store.commit('reset');

    store.commit('set_course_info', props.data);

    store.commit('set_current_index', props.currentIndex);
    props.data &&
      props.data.step > -1 &&
      store.commit('set_complete_index', props.data.step);

    let courseInfo = computed(() => store.state.courseInfo);

    let currentIndex = computed({
      get: () => store.state.currentIndex,
      set: (val) => store.commit('set_current_index', val),
    });

    let completeIndex = computed(() => store.state.completeIndex);

    let stepList = [
      { t: '基础信息', c: 'BasicComponent' },
      { t: '添加课次', c: 'CoursesComponent' },
      { t: '备课设置', c: 'SettingComponent' },
      { t: '完成', c: 'ReleaseComponent' },
    ];
    const changeStep = async (val) => {
      let res: any = await axios.post<null, { result: boolean; json: any }>(
        'course/changeCourseSetStep',
        { courseId: courseInfo.value.id, step: val }
      );
      if (res.result) {
        store.commit('set_complete_index', val);
      }
    };

    return { completeIndex, stepList, currentIndex, courseInfo, changeStep };
  },
  watch: {
    currentIndex: {
      handler: async function (val, oldVal) {
        if (oldVal > this.courseInfo.step && val !== this.stepList.length - 1) {
          this.changeStep(oldVal);
        }
        if (val === this.stepList.length - 1) {
          this.changeStep(val);
        }
      },
    },
  },
};
</script>

<style lang="scss" scoped>
.container {
  height: 100%;
  background: #f4f5f9;
  display: flex;
  flex-direction: column;
  .header {
    display: flex;
    height: 60px;
    padding-left: 70px;
    color: #fff;
    font-size: 18px;
    line-height: 60px;
    background: #72b22a;
    box-shadow: 0px -2px 6px 0px rgba(91, 125, 255, 0.08);
    & > span {
      cursor: pointer;
    }
    .btn-action {
      margin-left: auto;
      margin-right: 40px;
      .el-button {
        padding: 10px 34px;
        color: #fff;
        border-radius: 18px;
        background: rgba($color: #000000, $alpha: 0);
        &:active {
          border-color: #fff;
        }
        &:last-child {
          color: #72b22a;
          background: #fff;
        }
      }
    }
  }
  .content {
    display: flex;
    flex-direction: column;
    flex: 1 1 60px;
    width: 100%;
    max-width: 1120px;
    margin: 12px auto 0;
  }
  .steps {
    padding: 15px 45px;
    margin-bottom: 20px;
    background: #fff;
    border-radius: 6px;
    ul {
      height: 40px;
      display: flex;
      overflow: hidden;
    }
    li {
      flex: 1;
      color: #666;
      font-size: 18px;
      line-height: 40px;
      text-align: center;
      background: #f0f2f5;
      position: relative;
      cursor: pointer;
      pointer-events: none;
      &.is__disabled {
        pointer-events: none;
      }
      &.is__complete {
        color: #fff;
        background: #99d6d2;
        pointer-events: initial;
        i {
          display: inline-block;
        }
        &::before {
          display: none;
        }
      }
      &.is__current {
        color: #fff;
        background: #63c7c2;
      }
      &.is__complete,
      &.is__current {
        &::before {
          border-color: #fff;
        }
      }
      &:not(:first-child) {
        margin-left: -10px;
      }
      &:not(:last-child)::after {
        content: "";
        display: block;
        width: 40px;
        height: 28px;
        border-top: solid 5px #fff;
        border-right: solid 5px #fff;
        background: inherit;
        position: absolute;
        top: 0;
        right: 0;
        z-index: 2;
        margin-right: -10px;
        border-radius: 6px;
        transform: rotateZ(45deg);
      }
      &:first-child {
        border-top-left-radius: 6px;
        border-bottom-left-radius: 6px;
      }
      &:last-child {
        border-top-right-radius: 6px;
        border-bottom-right-radius: 6px;
      }
      &::before {
        content: attr(data-index);
        display: inline-block;
        width: 26px;
        height: 26px;
        margin-top: 7px;
        margin-right: 10px;
        line-height: 22px;
        color: inherit;
        font-size: 14px;
        text-align: center;
        border: solid 2px #666;
        border-radius: 50%;
        box-sizing: border-box;
        vertical-align: top;
      }
      i {
        display: none;
        width: 26px;
        height: 26px;
        color: #99d6d2;
        line-height: 26px;
        text-align: center;
        font-weight: bold;
        background: #fff;
        vertical-align: top;
        margin: 7px 10px 0 0;
        border-radius: 50%;
      }
    }
  }
}
</style>